﻿@model object
@{
    Layout = ViewBag.Layout ?? "_TR.cshtml";
    ViewData.TemplateInfo.HtmlFieldPrefix = ViewData.TemplateInfo.HtmlFieldPrefix.Replace(ViewData.ModelMetadata.PropertyName, "").Trim('.');
    var propertyName = ViewData["name"] == null ? ViewData.ModelMetadata.PropertyName : ViewData["name"].ToString();
    var id = ViewData.TemplateInfo.GetFullHtmlFieldId(propertyName);

    var editorHtmlAttributes = Html.GetUnobtrusiveValidationAttributes(propertyName, ViewData.ModelMetadata).Merge(ViewData.ModelMetadata.AdditionalValues, true);
    var htmlAttributes = ViewBag.HtmlAttributes;
    if (htmlAttributes != null)
    {
        if (htmlAttributes is IDictionary<string, object>)
        {
            editorHtmlAttributes.Merge((IDictionary<string, object>)htmlAttributes);
        }
        else
        {
            editorHtmlAttributes.Merge(new RouteValueDictionary(htmlAttributes));
        }
    }
}

@Html.TextBox(propertyName, Model, editorHtmlAttributes)
@Html.ValidationMessage(ViewData.ModelMetadata, new { name = ViewData["name"] })
@if (!string.IsNullOrEmpty(ViewData.ModelMetadata.Description))
{
    <em class="tip">@Html.Raw(ViewData.ModelMetadata.Description.Localize())</em>
}
<script>
    $(function () {
        $('#@id').autocomplete({
                    source: '@Html.Raw(Url.Action("Index", "AutoComplete", ViewContext.RequestContext.AllRouteValues().Merge("dataSourceType", ViewData.ModelMetadata.GetDataSourceType().AssemblyQualifiedName)))'
                });
            });
</script>
